<?php

namespace app\property\controller;

use app\property\model\ExamineVisitModel;
use app\property\model\PropertyVisitModel;
use think\admin\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Visit extends Controller
{
    /**
     * 当前操作数据库
     * @var string
     */
    private string $table = 'Visit';

    /**
     * 访客管理
     * @auth true
     * @menu true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $this->title = '访客管理';
        $query = $this->_query(PropertyVisitModel::class);
        // 加载对应数据
        $this->typeOf = $this->request->get('typeOf', 'index');
        if ($this->typeOf == 'index') $query->where(['is_deleted' => 0]);
        elseif ($this->typeOf == 'is_examine') $query->where(['status' => 0, 'is_deleted' => 0]);
        elseif ($this->typeOf == 'is_deleted') $query->where(['is_deleted' => 1]);
        $query->like('name,tel')->order('id desc')->page(true, true);
    }

    /**
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail()
    {


    }

    /**
     * 表单数据处理
     * @param array $data
     */
    protected function _form_filter(array &$data)
    {

    }

    /**
     * @auth true
     * @return void
     */
    public function examine()
    {
        if ($this->request->isPost()) {
            $par = $this->request->post();
            $par['create_time'] = $par['update_time'] = time();

            $model = new ExamineVisitModel();
            $res = $model->insert($par);
            $m = new PropertyVisitModel();
            $r = $m->where(['id' => $par['pid']])->save(['status' => $par['status']]);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $uid = $this->request->get("uid");
            $pid = $this->request->get("id");
            $admin = $this->app->session->get('user');
            if (!$admin) {
                $this->error('账号未登录');
            } else {
                $author = $admin['nickname'];
            }
            $data = [
                'uid' => $uid,
                'pid' => $pid,
                'author' => $author
            ];
            $this->fetch('', ['vo' => $data]);
        }
    }

    /**
     * 修改发布状态
     * @auth true
     * @throws DbException
     */
    public function state()
    {
        $this->_applyFormToken();
        $this->_save(PropertyVisitModel::class, $this->_vali([
            'status.in:-1,1' => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * @throws DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        $this->_save(PropertyVisitModel::class, $this->_vali([
            'is_deleted.in:0,1' => '状态值范围异常！',
            'is_deleted.require' => '状态值不能为空！',
        ]));
    }
}